package org.example.controller;


import org.example.entity.Student;
import org.example.form.SearchForm;
import org.example.form.StudentForm;
import org.example.service.StudentService;
import org.example.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-10-31
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @PostMapping("/save")
    public ResultVO save(@RequestBody Student student) {
        Boolean b = this.studentService.saveStudent(student);
        if (!b) { return ResultVO.error("保存失败"); }
        return ResultVO.success(null);
    }
    @GetMapping("/list/{page}/{size}")
    public ResultVO list(@PathVariable Integer page, @PathVariable  Integer size) {
        return ResultVO.success(this.studentService.list(page, size));
    }
    @GetMapping("/search")
        public ResultVO search(SearchForm searchForm) {
        return ResultVO.success(this.studentService.search(searchForm));
        }
    @GetMapping("/findById/{id}")
   public ResultVO findById(@PathVariable Integer id) {
        Student student = this.studentService.getById(id);
        StudentForm studentForm = new StudentForm();
        BeanUtils.copyProperties(student, studentForm);
        studentForm.setOrdDormitoryId(student.getDormitoryId());
        return ResultVO.success(studentForm);
    }
    @PutMapping("/update")
    public ResultVO update(@RequestBody StudentForm studentForm) {
        Boolean b = this.studentService.updateById(studentForm);
        if (!b) { return ResultVO.error("更新失败"); }
        return ResultVO.success(null);

    }
    @DeleteMapping("/deleteById/{id}")
    public ResultVO delete(@PathVariable Integer id) {
        Boolean b = this.studentService.deleteById(id);
        if (!b) { return ResultVO.error("删除失败"); }
        return ResultVO.success(null);
    }

}
